package cappuccino.couldbook.ui.Adapter;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cappuccino.couldbook.App;
import cappuccino.couldbook.R;
import cappuccino.couldbook.core.mananger.AccountM;
import cappuccino.couldbook.data.Group;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.net.NetApi;
import cappuccino.couldbook.net.Network;
import cappuccino.couldbook.ui.GroupActivity;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/12.
 */
public class RecylerGroupsAdapter extends RecyclerView.Adapter<RecylerGroupsAdapter.GroupViewHolder> {

    List<Group> groups;
    Activity activity;
    int status;
    public RecylerGroupsAdapter(Activity activity,List<Group> groups,int status){
        this.activity = activity;
        this.groups = groups;
        this.status = status;
    }

    @Override
    public GroupViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        GroupViewHolder holder;
        holder = new GroupViewHolder(LayoutInflater.from(App.getInstance().getApplicationContext()).inflate(R.layout.item_info, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(GroupViewHolder holder, int position) {
        holder.setInfo(position);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return groups.size();
    }



    class GroupViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView tv;
        ImageView iv;
        View view;
        public GroupViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            tv = (TextView) itemView.findViewById(R.id.tv_bar_text);
            iv = (ImageView) itemView.findViewById(R.id.iv_bar_photo);
            view.setOnClickListener(this);
        }

        public void setInfo(int pos) {
            tv.setText(groups.get(pos).name);
        }

        @Override
        public void onClick(View v) {
            if(status == 1){
                Intent intent = new Intent(activity, GroupActivity.class);
                intent.putExtra("groupid",groups.get(getLayoutPosition()).id);
                activity.startActivity(intent);
            } else {
                new AlertDialog.Builder(activity).setMessage("是否加入群组"+groups.get(getLayoutPosition()).name).setNegativeButton("否",null).setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        NetApi api = Network.getNetApi();
                        AccountM accountM = new AccountM(activity);
                        int uid = accountM.getAccout();
                        if(uid != -1)
                            api.applyGroup(uid,groups.get(getLayoutPosition()).id).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Result<Group>>() {
                                @Override
                                public void call(Result<Group> groupResult) {
                                    if(groupResult.status == 1)
                                        Toast.makeText(activity, "成功", Toast.LENGTH_SHORT).show();
                                    else
                                        Toast.makeText(activity, "加入失败,请确认您是否已在该群组", Toast.LENGTH_SHORT).show();
                                }
                            }, new Action1<Throwable>() {
                                @Override
                                public void call(Throwable throwable) {
                                    Toast.makeText(activity, "加入失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        else
                            Toast.makeText(activity,"请先登录",Toast.LENGTH_SHORT).show();
                    }
                }).show();
            }
        }
    }
}
